home *** CD-ROM | disk | FTP | other *** search
- Path: vixen.cso.uiuc.edu!usenet
- From: homer@uiuc.edu
- Newsgroups: comp.lang.c++
- Subject: Re: template with unknown classes!
- Date: 14 Mar 1996 20:09:39 GMT
- Organization: University of Illinois at Urbana
- Message-ID: <4i9ue3$9g0@vixen.cso.uiuc.edu>
- References: <3146F7D0.791D@nada.kth.se> <4i9gto$dtu@uuneo.neosoft.com>
- Reply-To: n-dade@uiuc.edu
- NNTP-Posting-Host: homer.apr.uiuc.edu
- X-Newsreader: IBM NewsReader/2 v1.2
-
- Wmatthew@lan-aces.com (W. Matthews) writes:
- >In article <3146F7D0.791D@nada.kth.se>, Andreas Swdrdh <da1-asw@nada.kth.se> says:
-
- >>// any success yet. The problem is that I don't want the whole class to be a
- >>// template, just THIS function. Is there a solution with templates or do
- >>// you have to use friend in some sort of way? Maybe use of dummy-class-pointer
- >>// just to get the right class type?! Would be most greatful for fast replies.
- >>
- >>// The possible function?!
- >>
- >>template<class object>void new_object() // or something like that!
- >>{
- >> if(!head)
- >> head = new object; // where object doesn't have to be known.
- >> ... // And the constuctor of object initializes it!
- >> ...
- >>}
-
- Have two classes: the first is a non-template class that contains everything
- about a listhandle except the new_object() function. Derive from that a
- template class that contains the new_object() function. ie
-
- class listhandlebase {
- void* head;
- public:
- listhandlebase() : head(NULL) {}
- ... all the other functions you want...
- };
-
- template<class OBJECT> class listhandle : public listhandlebase {
- public:
- new_object() {
- if (!head) { head = new OBJECT; }
- }
- };
-
- This way all the different listhandle classes share the code in listhandlebase
- (which I assume was the point of not wanting make the entire listhandle
- class a template class).
-
- -Nicolas Dade / n9rzb / nicolas-dade@uiuc.edu
-
-